package com.recruit.common.util;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.recruit.model.EducationDO;
import com.recruit.model.ExperienceDO;
import com.recruit.vo.ResumeVO;
import lombok.Data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author 陈竞超
 * @version 1.0
 * @qq 2250537249
 */

// 教育背景
class Education {
    private String school;
    private String major;
    private String degree;
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDateTime start_date;
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDateTime end_date;
    private String description;
    private boolean is_full_time;

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

    public LocalDateTime getStart_date() {
        return start_date;
    }

    public void setStart_date(LocalDateTime start_date) {
        this.start_date = start_date;
    }

    public LocalDateTime getEnd_date() {
        return end_date;
    }

    public void setEnd_date(LocalDateTime end_date) {
        this.end_date = end_date;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isIs_full_time() {
        return is_full_time;
    }

    public void setIs_full_time(boolean is_full_time) {
        this.is_full_time = is_full_time;
    }
}

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
class Experience {
    @JsonProperty("internships")
    private List<Internship> internships = new ArrayList<>();

    @JsonProperty("projects")
    private List<Project> projects = new ArrayList<>();

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Internship {
        @JsonProperty("company")
        private String company;

        @JsonProperty("position")
        private String position;

        @JsonProperty("start_date")
        private String startDate;

        @JsonProperty("end_date")
        private String endDate;

        @JsonProperty("description")
        private String description;

        @JsonProperty("duration_months")
        private Integer durationMonths;
    }

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Project {
        @JsonProperty("name")
        private String name;

        @JsonProperty("start_date")
        private String startDate;

        @JsonProperty("end_date")
        private String endDate;

        @JsonProperty("background")
        private String background;

        @JsonProperty("description")
        private String description;

        @JsonProperty("tech_stack")
        private List<String> techStack = new ArrayList<>();

        @JsonProperty("role")
        private String role;
    }
}

public class ResumeDOCConverter {
    public static Map<String, Object> ResumeConverter(ResumeVO resumeVO) {
        Map<String, Object> resumeData = new HashMap<>();
        // meta板块
        Map<String, Object> metaData = new HashMap<>();
        metaData.put("source_file",null);
        metaData.put("parse_time",null);
        metaData.put("job_target",resumeVO.getJobIntention());
        metaData.put("residence",resumeVO.getHome());
        metaData.put("english_level",resumeVO.getEnglish());
        metaData.put("age",resumeVO.getAge());
        if(resumeVO.getSex() == 1){
            metaData.put("gender","女");
        }
        else{
            metaData.put("gender","男");
        }
        // 技能
        List<String> skills = new ArrayList<>();
        String abilities = resumeVO.getAbility();
        String[] ability = abilities.split(",");
        for(int x = 0; x < ability.length; x++) {
            skills.add(ability[x]);
        }
        List<String> raw_skills = new ArrayList<>();
        metaData.put("skills",skills);
        metaData.put("summary",resumeVO.getPersonalSummary());
        metaData.put("raw_skills",raw_skills);
        resumeData.put("meta", metaData);
        // eduction板块
        List<Education> educationData = new ArrayList<>();
        List<EducationDO> educations = resumeVO.getEducations();
        for(int x = 0; x < educations.size(); x++) {
            Education education = new Education();
            education.setSchool(educations.get(x).getName());
            education.setMajor(educations.get(x).getMajor());
            if(educations.get(x).getName().contains("大学")) {
                education.setDegree("本科");
            }
            else{
                education.setDegree("专科");
            }
            education.setStart_date(LocalDateTime.now());
            education.setEnd_date(LocalDateTime.now());
            education.setDescription(educations.get(x).getDescriptions());
            education.setIs_full_time(true);
            educationData.add(education);
        }
        resumeData.put("education", educationData);
        // experience板块
        Experience experience = new Experience();
        for(int x = 0; x < resumeVO.getExperiences().size(); x++) {
            Experience.Internship internship = new Experience.Internship();
            internship.setCompany(resumeVO.getExperiences().get(x).getName());
            internship.setPosition(resumeVO.getExperiences().get(x).getPosition());
            internship.setStartDate(LocalDateTime.now().toString());
            internship.setEndDate(LocalDateTime.now().toString());
            internship.setDescription(resumeVO.getExperiences().get(x).getPerformance());
            internship.setDurationMonths(null);
            experience.getInternships().add(internship);
        }
        for(int x = 0; x < resumeVO.getProjects().size(); x++) {
            Experience.Project project = new Experience.Project();
            project.setName(resumeVO.getProjects().get(x).getName());
            project.setStartDate(LocalDateTime.now().toString());
            project.setEndDate(LocalDateTime.now().toString());
            project.setBackground(resumeVO.getProjects().get(x).getBackground());
            project.setTechStack(new ArrayList<>());
            project.setTechStack(null);
            experience.getProjects().add(project);
        }
        resumeData.put("experience", experience);
        return resumeData;
    }
    public static String safeParseDate(String input) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+0")); // 格式化时修改时区
            Date date = sdf.parse(input);
            return new SimpleDateFormat("yyyy-MM-dd").format(date);
        } catch (ParseException e) {
            // 记录错误日志
            System.err.println("日期解析失败: " + input);
            e.printStackTrace();
            // 返回默认值或抛出自定义异常
            return "1970-01-01";
        }
    }

}


